package chatservice;

import java.util.Vector;
import pojo.*;
import dao.*;

public class RoomServiceImpl implements IRoomService {
	
	private RoomDao rd;
	private UserServiceImpl  us;
	private ClassEntity ce;
	public RoomServiceImpl()
	{
		rd=new RoomDao();
		us=new UserServiceImpl();
		ce=ClassEntity.getInstance();
	}
	public Vector<String> getAllRooms()
	{
		Vector<String> roomlist=rd.getAllRooms();
		return roomlist;
	}
	public int  createRoom(String username,String roomname)
	{
		int roomid=0;
		rd.createRoom(roomname);//创建房间
		roomid=rd.getRoomId(roomname);
		us.setUserRoomId(username, roomid);//修改用户roomid
		ce.addUser(roomid, username);//房间列表初始化
		return roomid;
	}
	public Vector<String>  selectRoom(String username,String roomname)
	{
		int roomid=rd.getRoomId(roomname);
		//System.out.println("Server Run roomid "+roomid);
		
		us.setUserRoomId(username, roomid);// 设置用户的房间号码
		ce.addUser(roomid, username);//将用户加入某个房间
		return ce.getUserlist(roomid);//返回房间用户列表
	}
	public Vector<String>   changeRoom(String username,String roomname)
	{
		int roomid=rd.getRoomId(roomname);//得到房间号码
		us.setUserRoomId(username, 1);//将用户的roomid置为1
		ce.delUser(roomid, username);//从房间中删除用户
		return this.getAllRooms();
	}
	public Vector<String> getUsersByRoomId(int roomid)
	{
		Vector<String> userlist=ce.getUserlist(roomid);
		return userlist;
	}
	public Vector<String> getUsersByRoomName(String roomname)
	{
		int roomid=rd.getRoomId(roomname);
		Vector<String> userlist=ce.getUserlist(roomid);
		return userlist;
	}
}
